package com.braveman.im.ui.fragment;

import android.os.Bundle;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;

import com.braveman.im.R;
import com.braveman.im.ui.activity.message.ChatAllActivity;
import com.ht.baselibs.ui.base.BaseFragment;
import com.ht.baselibs.utils.ActivityUtils;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMConversation;
import com.hyphenate.easeui.widget.EaseConversationList;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

public class MessageFragFragment extends BaseFragment {
    private EaseConversationList conversationListView;
    protected List<EMConversation> conversationList = new ArrayList<>();
    protected List<EMConversation> conversationListForPerson = new ArrayList<>();
    protected List<EMConversation> conversationListForGroup = new ArrayList<>();
    protected List<EMConversation> conversationListForCompany = new ArrayList<>();

    @Override
    protected boolean isStaticPage() {
        return true;
    }

    @Override
    protected View initContentView() {
        return LayoutInflater.from(mContext).inflate(R.layout.frag_message, null);
    }

    @Override
    protected void initChildView() {

    }

    @Override
    protected void load() {

    }
    private void DoNetWorkgetAllMessage() {
        conversationList.clear();
        conversationList.addAll(loadConversationList());

        conversationListForGroup.clear();
        conversationListForPerson.clear();
        conversationListForCompany.clear();

        for (EMConversation em : conversationList) {
            switch (em.getType()) {
                case Chat:
                    conversationListForPerson.add(em);
                    break;
                case GroupChat:
                    conversationListForGroup.add(em);
                    break;
                case ChatRoom:
                    conversationListForCompany.add(em);
                    break;
            }

        }
    }

    @Override
    protected void setChildViewListener() {

    }

    private void reLoad(int states) {
        switch (states) {
            case 0:
                load();
                conversationListView = (EaseConversationList) getView().findViewById(R.id.list_ease_conversation);
                conversationListView.init(conversationList);
                conversationListView.refresh();
                conversationListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                        Bundle bundle = new Bundle();
                        bundle.putInt("states", 3);//1群聊 2 单位 3单聊
                        bundle.putString("user_id", conversationList.get(i).conversationId());
                        ActivityUtils.startActivity(mContext, ChatAllActivity.class, bundle);
                    }
                });

                break;
            case 1:
                load();
                conversationListView = (EaseConversationList) getView().findViewById(R.id.list_ease_conversation);
                conversationListView.init(conversationListForGroup);
                conversationListView.refresh();
                conversationListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                        Bundle bundle = new Bundle();
                        bundle.putInt("states", 1);//1群聊 2 单位 3单聊
                        bundle.putString("user_id", conversationListForGroup.get(i).conversationId());
                        ActivityUtils.startActivity(mContext, ChatAllActivity.class, bundle);
                    }
                });
                break;
            case 2:
                load();
                conversationListView = (EaseConversationList) getView().findViewById(R.id.list_ease_conversation);
                conversationListView.init(conversationListForCompany);
                conversationListView.refresh();
                conversationListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                        Bundle bundle = new Bundle();
                        bundle.putInt("states", 2);//群聊 2 单位 3单聊
                        bundle.putString("user_id", conversationListForCompany.get(i).conversationId());
                        ActivityUtils.startActivity(mContext, ChatAllActivity.class, bundle);
                    }
                });

                break;
            case 3:
                load();
                conversationListView = (EaseConversationList) getView().findViewById(R.id.list_ease_conversation);
                conversationListView.init(conversationListForPerson);
                conversationListView.refresh();
                conversationListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                        Bundle bundle = new Bundle();
                        bundle.putInt("states", 3);//群聊 2 单位 3单聊
                        bundle.putString("user_id", conversationList.get(i).conversationId());
                        ActivityUtils.startActivity(mContext, ChatAllActivity.class, bundle);
                    }
                });
                break;
            case 4:

                break;
            case 5:


                break;
            case 6:


                break;
            case 7:
//                showListFragment();
                break;
        }

    }


    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
        }
    }

    protected List<EMConversation> loadConversationList() {
        // get all conversations
        Map<String, EMConversation> conversations = EMClient.getInstance().chatManager().getAllConversations();
        List<Pair<Long, EMConversation>> sortList = new ArrayList<Pair<Long, EMConversation>>();
        /**
         * lastMsgTime will change if there is new message during sorting
         * so use synchronized to make sure timestamp of last message won't change.
         */
        synchronized (conversations) {
            for (EMConversation conversation : conversations.values()) {
                if (conversation.getAllMessages().size() != 0) {
                    sortList.add(new Pair<Long, EMConversation>(conversation.getLastMessage().getMsgTime(), conversation));
                }
            }
        }
        try {
            // Internal is TimSort algorithm, has bug
            sortConversationByLastChatTime(sortList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        List<EMConversation> list = new ArrayList<EMConversation>();
        for (Pair<Long, EMConversation> sortItem : sortList) {
            list.add(sortItem.second);
        }
        return list;
    }

    private void sortConversationByLastChatTime(List<Pair<Long, EMConversation>> conversationList) {
        Collections.sort(conversationList, new Comparator<Pair<Long, EMConversation>>() {
            @Override
            public int compare(final Pair<Long, EMConversation> con1, final Pair<Long, EMConversation> con2) {

                if (con1.first.equals(con2.first)) {
                    return 0;
                } else if (con2.first.longValue() > con1.first.longValue()) {
                    return 1;
                } else {
                    return -1;
                }
            }

        });
    }


}
